package org.dromara.neutrinoproxy.server.util;

import freemarker.ext.beans.BeansWrapperBuilder;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateExceptionHandler;
import org.apache.commons.io.output.WriterOutputStream;

import java.io.*;
import java.nio.charset.StandardCharsets;

public class FreemarkerUtils {


    public static String format(Configuration cfg, String templateFile, Object data) {
        Template temp = null;
        try {
            temp = cfg.getTemplate(templateFile);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        StringWriter writer = new StringWriter();
        WriterOutputStream stream = new WriterOutputStream(writer, StandardCharsets.UTF_8);
        Writer out = new OutputStreamWriter(stream);
        try {
            temp.process(data, out);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return writer.toString();
    }


    /**
     * 初始化
     * @param templateFile
     */
    public static String format(File templateFolder, String templateFile, Object data){
        // Create your Configuration instance, and specify if up to what FreeMarker
        // version (here 2.3.22) do you want to apply the fixes that are not 100%
        // backward-compatible. See the Configuration JavaDoc for details.
        Configuration cfg = new Configuration(Configuration.VERSION_2_3_22);
        cfg.setObjectWrapper(new BeansWrapperBuilder(Configuration.VERSION_2_3_22).build());

        // Specify the source where the template files come from. Here I set a
        // plain directory for it, but non-file-system sources are possible too:
        try {
            cfg.setDirectoryForTemplateLoading(templateFolder);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        // Set the preferred charset template files are stored in. UTF-8 is
        // a good choice in most applications:
        cfg.setDefaultEncoding("UTF-8");

        // Sets how errors will appear.
        // During web page *development* TemplateExceptionHandler.HTML_DEBUG_HANDLER is better.
        cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);

        Template temp = null;
        try {
            temp = cfg.getTemplate(templateFile);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        StringWriter writer = new StringWriter();
        WriterOutputStream stream = new WriterOutputStream(writer, StandardCharsets.UTF_8);
        Writer out = new OutputStreamWriter(stream);
        try {
            temp.process(data, out);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return writer.toString();
    }




    /**
     * 用freemarker框架注入字符串
     */
    public static String formatByContent(String in, Object params) throws Exception {
        Configuration cfg = new Configuration(Configuration.VERSION_2_3_22);
        cfg.setObjectWrapper(new BeansWrapperBuilder(Configuration.VERSION_2_3_22).build());

        if(params == null){
            return in;
        }
        Template t = null;
        try {
            t = new Template("templateName", new StringReader(in), cfg);
        } catch (IOException e) {
            e.printStackTrace();
        }
        Writer out = new StringWriter();
        t.process(params, out);
        String content = out.toString();

        return content;
    }

}
